# coding=utf-8
"""
Created on 2015-12-10 @author: Eastmount
利用Selenium爬取百度百科5A级景区的内容介绍的代码,通过JIEBA分词工具进行分词，
最后通过wordcloud生成词云*.png图片
"""

import time
import re
import os
import sys
import codecs
import shutil
import jieba
import jieba.analyse
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.action_chains import ActionChains
from create_wordcloud import create_wordcloud

# Open PhantomJS
# driver = webdriver.PhantomJS(executable_path="D:\phantomjs-2.1.1-windows\sbin\phantomjs.exe")
driver = webdriver.PhantomJS(executable_path="D:\phantomjs-1.9.8-windows\phantomjs.exe")
# driver = webdriver.Firefox()
wait = ui.WebDriverWait(driver, 10)  # 显示等待时间(实例，最大等待时间)


def getInfobox(entityName, fileName):

    '''获取一个需搜索的信息'''

    try:
        # create paths and txt files
        print(u'文件名称: ', fileName)
        info = codecs.open(fileName, 'w', 'utf-8')

        # locate input  notice: 1.visit url by unicode 2.write files
        # Error: Message: Element not found in the cache
        #       Perhaps the page has changed since it was looked up
        # 解决方法: 使用Selenium和Phantomjs

        print(u'实体名称: ', entityName.rstrip('\n'))
        driver.get("http://baike.baidu.com/")
        elem_inp = driver.find_element_by_xpath("//form[@id='searchForm']/input")
        # elem_inp = driver.find_elements_by_xpath("//div[@class='lemma-summary']/div")
        elem_inp.send_keys(entityName)  # 将信息输入到文本框,相当于输入value的值
        elem_inp.send_keys(Keys.RETURN)
        # info.write(entityName.rstrip('\n') + '\r\n')  # codecs不支持'\n'换行

        # load content 摘要
        elem_value = driver.find_elements_by_xpath("//div[@class='lemma-summary']/div")
        for value in elem_value:
            print(value.text)
            info.writelines(value.text + '\r\n')

        # 爬取文本信息
        # 爬取所有段落<div class='para'>的内容 class='para-title'为标题 [省略]
        time.sleep(2)
    # except Exception as e:  # 'utf8' codec can't decode byte
    #     print("Error: ", e)
    finally:
        print('\n')
        info.close()


def main():

    '''通过5A文件确定关键字搜索'''

    path = "BaiduSpider\\"
    if os.path.isdir(path):
        shutil.rmtree(path, True)
    os.makedirs(path)
    source = open("Tourist_spots_5A.txt", 'r')
    num = 1
    for entityName in source:
        # entityName = unicode(entityName, "utf-8")
        if u'故宫' in entityName:
            entityName = '北京故宫'
        else:
            entityName = entityName.rstrip('\n')
        name = "%04d" % num
        fileName = path + str(name) + ".txt"
        getInfobox(entityName, fileName)
        num = num + 1
    print('End Read Files!')
    source.close()
    driver.close()


# 导入自定义词典
jieba.load_userdict("dict_baidu.txt")


def read_file_cut():

    '''创建分词路径和文件'''

    path = "BaiduSpider\\"
    respath = "BaiduSpider_Result\\"
    if os.path.isdir(respath):
        shutil.rmtree(respath, True)  # 递归删除路径目录
    os.makedirs(respath)

    num = 1  # num .text文件数
    while num <= 1:
        name = "%04d" % num  # 0 用0补位 d 10进制
        fileName = path + str(name) + ".txt"
        resName = respath + str(name) + ".txt"
        source = open(fileName, 'r', encoding='utf-8')
        if os.path.exists(resName):
            os.remove(resName)
        result = codecs.open(resName, 'w', 'utf-8')
        line = source.readline()
        line = line.rstrip('\n')

        while line != "":
            # line = unicode(line, "utf-8")
            seglist = jieba.cut(line, cut_all=False)  # 精确模式
            output = '|'.join(list(seglist))  # 空格拼接
            print(output)
            result.write(output + '\r\n')
            line = source.readline()
        else:
            print('End file: ' + str(num))
            source.close()
            result.close()
        num = num + 1
    else:
        print('End All')


if __name__ == '__main__':
    main()
    read_file_cut()
    create_wordcloud()


